#include <LocalSettings.au3>
#include <ScreenCapture.au3>
#include <Inet.au3>

Const $ProjectName = "NetView"

OpenAccess()

CreateFromSourceControl()

If Not Compile() Then
;	MsgBox(64,"Info", "Compile not ok")
	Exit
EndIf


If Not RunTests() Then
;	MsgBox(64,"Info", "Testrun failed")
	Exit
EndIf

SendInfoMail("Build succeeded")

Exit


Func RunTests()
	ActivateVBE()
	InsertModule(GetTestHelperModuleContent())
	Send("{F5}")
	If WinWaitActive("Quellcodeverwaltung", "", 30) <> 0 Then
		Send("{ESC}")
	EndIf
	If WinWaitActive("Test Run Result", "", 10) <> 0 Then
		$ResultString = WinGetText("","")
		Send("{ESC}")
		Local $Passed = GetPassed($ResultString)
		If $Passed Then
			Return True
		Else
			SendErrorMail("Tests failed")
			Return False
		EndIf
	EndIf
EndFunc

Func GetPassed($MsgBoxText)
	$parts = StringSplit($MsgBoxText, "OK" & @LF & "TestRun ", 1)
	$parts = StringSplit($parts[2], " in ", 1)
	Local $passed = ($parts[1] = "passed")
	Return $passed
EndFunc

Func InsertModule($Content)
	Send("!EM")
	Send("!BU")
	Send($Content, 1)
EndFunc

Func Compile()
	ActivateVBE()
	Send("!G{ENTER}")
	If WinWaitActive("Quellcodeverwaltung","",5) = 0 Then
		Return True
	Else
		Send("{ALTDOWN}N{ALTUP}")	; Do not checkout the module with the error
		WinWaitActive("Microsoft Visual Basic")
		$ErrorMessage = GetErrorMessage(WinGetText("",""))
		SaveScreenShot()
		Send("{ENTER}")
		SendErrorMail("Compile Error: " & $ErrorMessage)
		Return False
	EndIf
EndFunc

Func SendInfoMail($Message)
	SendErrorMail($Message)
EndFunc

Func SendErrorMail($Message)
	MsgBox(64,"Mail fake", $Message)
	If _INetSmtpMail("smtp.gmail.com", "NetView Build", "paul.rohorzka@softconcept.at", "paul.rohorzka@softconcept.at", $Message, "I'm sorry") = 0 Then
		MsgBox(64, "Info", "Geht net:" & @CRLF & @error)
	EndIf
EndFunc

Func GetErrorMessage($MsgBoxText)
	Return StringRegExpReplace($MsgBoxText, "OK\nHelp\nFehler beim Kompilieren:\n\n(.*)", "$1")
EndFunc

Func SaveScreenShot()
	_ScreenCapture_Capture("C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\Screenshot.png")
EndFunc

Func ActivateVBE()
	WinActivate("Microsoft Access")
	Send("{ALTDOWN}{F11}{ALTUP}")

	$stillLooking = True
	While $stillLooking
		$activeWindowTitle = WinGetTitle(WinActive(""))
		If $activeWindowTitle == "MZ-Tools 3.0" Then
			Send("{ENTER}")
			$stillLooking = False
		ElseIf StringInStr($activeWindowTitle, "Microsoft Visual Basic - NetView") = 1 Then
			$stillLooking = False
		EndIf
		sleep(5)
	WEnd
	WinWaitActive("Microsoft Visual Basic - NetView")
EndFunc

Func CreateFromSourceControl()
	Send("!XAD")

	WinWaitActive("Connect to a Team Foundation Server")
	Send("{ENTER}")

	WinWaitActive("Choose Folder in Team Foundation Server")
	Send("{ALT}F{RIGHT}{DOWN}{RIGHT}")
	Sleep(500)
	Send("NetView")	; Select the folder containing the Access sources
	Send("{ENTER}")

	WinWaitActive("Quellcodeverwaltung")
	Send("{ALTDOWN}J{ALTUP}")
	Sleep(500)	; Wait for the MessageBox to disappear since the next expected window will have the same title

	WinWaitActive("Quellcodeverwaltung")
	;MsgBox(64, "Info", "Success Message is open")
	Send("{ENTER}")
EndFunc

Func OpenAccess()
	; This definition must be contained in the Include LocalSettings.au3
;	Const $AccessPath = "C:\Program Files\Microsoft Office\Office11\msaccess.exe"
	Local $pId
	$pId = Run($AccessPath)
	If $pId = 0 Then
		MsgBox(64,"Info", "Could not open Microsoft Access")
		Exit
	EndIf
	WinWaitActive("Microsoft Access")
	Maximize()
EndFunc

Func Maximize()
	Send("{Alt}{SPACE}X")
EndFunc

Func GetTestHelperModuleContent()
	Return "Option Compare Database" & @CRLF & _
"Option Explicit" & @CRLF & _
"" & @CRLF & _
"Public Sub RunAllTests()" & @CRLF & _
"   Dim TestSuite As AccessTestSuite" & @CRLF & _
"Dim Summary As Object 'ITestSummary" & @CRLF & _
"Dim SummaryInfo As String" & @CRLF & _
"" & @CRLF & _
"Set TestSuite = New AccessTestSuite" & @CRLF & _
"Set TestSuite.HostApplication = Access.Application" & @CRLF & _
"" & @CRLF & _
"Set Summary = TestSuite.AddFromVBProject.Run.Summary" & @CRLF & _
"" & @CRLF & _
"SummaryInfo = ""TestRun "" & IIf(Summary.Failed > 0, ""failed"", ""passed"") & "" in "" & Summary.ElapsedTime & ""ms."" & vbNewLine & vbNewLine & _" & @CRLF & _
"              ""Passed: "" & Summary.Passed & vbNewLine & _" & @CRLF & _
"""Failed: "" & Summary.Failed & vbNewLine & _" & @CRLF & _
"""Ignored: "" & Summary.Ignored & vbNewLine & _" & @CRLF & _
"""Total: "" & Summary.Total" & @CRLF & _
"" & @CRLF & _
"MsgBox SummaryInfo, , ""Test Run Result"""
	;"End Sub"	; Don't include it here since it will be included automatically by the VBE
EndFunc
